package edu.unindra.elearning.form.addin;

import java.awt.Component;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import edu.unindra.elearning.model.TableModelMateri;
import edu.unindra.elearning.util.ViewPortGlass;
import usu.widget.ButtonGlass;
import usu.widget.Panel;
import usu.widget.util.WidgetUtilities;

public class ELearningFormMainAdminMateri extends Panel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private TableModelMateri modelTable;
	private JTable table;

	private ButtonGlass btnAdd;
	private ButtonGlass btnDelete;
	private ButtonGlass btnEdit;
	private ButtonGlass btnRefresh;

	private ViewPortGlass viewPort;
	private JScrollPane scrollPane;

	public ELearningFormMainAdminMateri() {
		modelTable = new TableModelMateri();

		initComponents();
		initFinals();
	}

	/**
	 * 
	 * @return
	 */
	public ButtonGlass getBtnRefresh() {
		return btnRefresh;
	}

	/**
	 * 
	 * @return
	 */
	public ButtonGlass getBtnAdd() {
		return btnAdd;
	}

	/**
	 * 
	 * @return
	 */
	public ButtonGlass getBtnDelete() {
		return btnDelete;
	}

	/**
	 * 
	 * @return
	 */
	public ButtonGlass getBtnEdit() {
		return btnEdit;
	}

	/**
	 * 
	 * @return
	 */
	public TableModelMateri getModelTable() {
		return modelTable;
	}

	/**
	 * 
	 * @return
	 */
	public JTable getTable() {
		return table;
	}

	private void initComponents() {
		scrollPane = new JScrollPane();
		viewPort = new ViewPortGlass();
		table = new JTable();

		btnAdd = new ButtonGlass();
		btnDelete = new ButtonGlass();
		btnEdit = new ButtonGlass();
		btnRefresh = new ButtonGlass();

		viewPort.setView(table);

		table.setAutoCreateRowSorter(true);
		table.setForeground(new java.awt.Color(255, 255, 255));
		table.setToolTipText("Tabel Materi");
		table.setFillsViewportHeight(true);
		table.setOpaque(false);
		table.setSelectionBackground(new java.awt.Color(51, 255, 52));
		table.setSelectionForeground(new java.awt.Color(0, 0, 0));

		setOpaque(false);
		setOpaqueGradient(false);
		setOpaqueImage(false);

		scrollPane.setBorder(javax.swing.BorderFactory
				.createLineBorder(new java.awt.Color(0, 0, 0)));
		scrollPane.setOpaque(false);
		scrollPane.setViewport(viewPort);

		btnDelete.setMnemonic('H');
		btnDelete.setText("Hapus");
		btnDelete.setToolTipText("Klik untuk menghapus materi");
		btnDelete.setRoundRect(true);

		btnEdit.setMnemonic('U');
		btnEdit.setText("Ubah");
		btnEdit.setToolTipText("Klik untuk mengubah materi");
		btnEdit.setRoundRect(true);

		btnAdd.setMnemonic('A');
		btnAdd.setText("Tambah");
		btnAdd.setToolTipText("Klik untuk menambah materi");
		btnAdd.setRoundRect(true);

		btnRefresh.setMnemonic('S');
		btnRefresh.setText("Segarkan");
		btnRefresh.setToolTipText("Klik untuk meload ulang database");
		btnRefresh.setRoundRect(true);
		btnRefresh.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				buttonRefreshActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING)
												.addComponent(
														scrollPane,
														javax.swing.GroupLayout.Alignment.LEADING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														597, Short.MAX_VALUE)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		btnRefresh,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																		317,
																		Short.MAX_VALUE)
																.addComponent(
																		btnAdd,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		btnEdit,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		btnDelete,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE)))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addComponent(scrollPane,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										327, Short.MAX_VALUE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														btnDelete,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														btnEdit,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														btnAdd,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														btnRefresh,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addContainerGap()));
	}

	private void initFinals() {
		table.setModel(modelTable);
		table.getTableHeader().setReorderingAllowed(false);
		WidgetUtilities.setAutomaticPopUpMenu(table);
		for (int i = 0; i < table.getColumnCount(); i++) {
			table.getColumnModel().getColumn(i)
					.setCellRenderer(new RenderTable());
		}
	}

	public static class RenderTable extends DefaultTableCellRenderer {

		private static final long serialVersionUID = -1;

		@Override
		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			if (value != null) {
				setToolTipText(value.toString());
				setOpaque(isSelected);
				return super.getTableCellRendererComponent(table, value,
						isSelected, hasFocus, row, column);
			} else {
				return super.getTableCellRendererComponent(table, value,
						isSelected, hasFocus, row, column);
			}
		}
	}

	private void buttonRefreshActionPerformed(java.awt.event.ActionEvent evt) {
		modelTable.removeAllElements();
	}
}
